/*
	File:		Player.cpp
	Author:		Ben Feldmann
	Purpose:	Class for managing game player
*/

#include "Player.h"
#include "Game.h"
#include "BaseGun.h"
#include "Animation.h"

#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"

Player::Player() 
{
	health				= 300;
	curWeapon			= 2;
	skillOne			= 1;
	skillTwo			= 2;
	moveSpeed			= 0.0f;
	weapon1				= nullptr;
	scrapMetal			= 0;
	circuitry			= 0;


	type = OBJ_PLAYER;
	SetX(100.0f);
	SetY(100.0f);
	SetWidth(64);
	SetHeight(64);
	SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/Dummy Images/player.png")));

	prevX = GetX();
	prevY = GetY();
	collided = false;

	/*player = new Animation;
	player->LoadAnimationFromFile("SwordSwingAnimation.xml");*/

	health				= 300;
	curWeapon			= 1;
	skillOne			= 1;
	skillTwo			= 2;
	moveSpeed			= 0.0f;
	weapon1				= nullptr;
	weapon2				= new BaseGun(this);
	scrapMetal			= 0;
	circuitry			= 0;

	weapon2	= new BaseGun(this);

}

Player::~Player()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}

	delete weapon2;
}

void Player::UseSkill(int skill) 
{
	if(skill == 1)
	{

	}
	else if(skill == 2)
	{

	}
}

void Player::Attack() 
{
	if(curWeapon == 1)
	{
		player.Play(false, 5.0f);
		//weapon1->Attack();
	}
	else if(curWeapon == 2)
	{
		weapon2->Attack();
	}
}

void Player::SpecialAttack() 
{
	if(curWeapon == 1)
	{
		
		//weapon1->SpecialAttack();
	}
	else if(curWeapon == 2)
	{
		weapon2->SpecialAttack();
	}
}

void Player::Update(float elapsed)
{
	CSGD_DirectInput* di = CSGD_DirectInput::GetInstance();


	// Movement

	/* CARDINAL */

	// North
	if(di->JoystickGetLStickXNormalized() < .5f && di->JoystickGetLStickXNormalized() > -.5f && di->JoystickGetLStickYNormalized() < 0.0f)
		SetY(GetY()-100*elapsed);
	// East
	else if(di->JoystickGetLStickYNormalized() > -.5f && di->JoystickGetLStickYNormalized() < .5f && di->JoystickGetLStickXNormalized() > 0.0f)
		SetX(GetX()+100*elapsed);
	// South
	else if(di->JoystickGetLStickXNormalized() > -.5f && di->JoystickGetLStickXNormalized() < .5f && di->JoystickGetLStickYNormalized() > 0.0f)
		SetY(GetY()+100*elapsed);
	// West
	else if(di->JoystickGetLStickYNormalized() < .5f && di->JoystickGetLStickYNormalized() > -.5f && di->JoystickGetLStickXNormalized() < 0.0f)
		SetX(GetX()-100*elapsed);

	//////////////////////

	/* ORDINAL */

	//Northeast
	else if(di->JoystickGetLStickXNormalized() > .5f && di->JoystickGetLStickYNormalized() < -.5f)
	{
		SetY(GetY()-50*elapsed); SetX(GetX()+50*elapsed);
	}
	// Southeast
	else if(di->JoystickGetLStickXNormalized() > .5f && di->JoystickGetLStickYNormalized() > .5f)
	{
		SetY(GetY()+50*elapsed); SetX(GetX()+50*elapsed);
	}
	// SouthWest
	else if(di->JoystickGetLStickXNormalized() < -.5f && di->JoystickGetLStickYNormalized() > .5f)
	{
		SetY(GetY()+50*elapsed); SetX(GetX()-50*elapsed);
	}
	// Northwest
	else if(di->JoystickGetLStickXNormalized() < -.5f && di->JoystickGetLStickYNormalized() < .5f)
	{
		SetY(GetY()-50*elapsed); SetX(GetX()-50*elapsed);
	}


	//////////////////////////////////////////////////////////////


	// Actions

	/* Attack */
	if(di->JoystickButtonPressed(0) || di->KeyPressed(DIK_A))
	{
		Attack();
	}

	/* Special Attack */
	if(di->JoystickButtonPressed(3))
	{
		SpecialAttack();
	}

	/* Switch Weapons */
	if(di->JoystickButtonPressed(2))
	{
		if(curWeapon == 1)
			curWeapon = 2;

		else if(curWeapon == 2)
			curWeapon = 1;
	}

	/* Skill One */
	if(di->JoystickButtonPressed(4))
	{
		UseSkill(1);
	}

	/* Skill Two */
	if(di->JoystickButtonPressed(5))
	{
		UseSkill(2);
	}


	//////////////////////////////////////////////////////////////


	// Check Wall Collision

	if(GetX() < 64)
		SetX(64.0);
	if(GetX() + GetWidth() > Game::GetInstance()->GetScreenWidth() - 64)
		SetX((float)Game::GetInstance()->GetScreenWidth() - (64 + GetWidth()));

	if(GetY() < 64)
		SetY(64.0);
	if(GetY() + GetHeight() > Game::GetInstance()->GetScreenHeight() - 64)
		SetY((float)Game::GetInstance()->GetScreenHeight() - (64 + GetHeight()));


	if(collided == false)
	{
		prevX = GetX();
		prevY = GetY();
	}
	collided = false;

	player.Update(elapsed);

}

void Player::Render()
{
	//CSGD_Direct3D::GetInstance()->GetSprite()->Flush();

	/*if(GetTextureID() != -1)
	{
		CSGD_TextureManager::GetInstance()->Draw(GetTextureID(), int(GetX()), int(GetY()));
	}

	CSGD_Direct3D::GetInstance()->DrawRect(GetRect(),255,255,255);*/

	player.Render(int(GetX()), int(GetY()), 1.0f, D3DCOLOR_XRGB(255, 255, 255), false);
}

bool Player::CheckCollision(IEntity* base) 
{
	if(Entity::CheckCollision(base) == true)
	{
		SetX(prevX); 
		SetY(prevY); 
		collided = true;
		return true;
	}
	else
	{
		collided = false;
		return false;
	}
}

